package demo1;

class BTNode {
    public char val;
    public BTNode left;
    public BTNode right;
    public BTNode(char val) {
        this.val = val;
    }
}

public class BinaryTree {

    public BTNode createBinaryTree() {
        BTNode btNode1 = new BTNode('A');
        BTNode btNode2 = new BTNode('B');
        BTNode btNode3 = new BTNode('D');
        BTNode btNode4 = new BTNode('E');
        BTNode btNode5 = new BTNode('H');
        BTNode btNode6 = new BTNode('C');
        BTNode btNode7 = new BTNode('F');
        BTNode btNode8 = new BTNode('G');
        btNode1.left = btNode2;
        btNode1.right = btNode6;
        btNode2.left = btNode3;
        btNode2.right = btNode4;
        btNode4.right = btNode5;
        btNode6.left = btNode7;
        btNode6.right = btNode8;
        return btNode1;
    }

    //前序遍历
    public void preOrder (BTNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder (BTNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //后序遍历
    public void postOrder (BTNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
}
